package petertel.pbx.utils;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;

public class Node {

	private List<Node> children = new ArrayList<Node>();
	private int lineno;
	private String line;

	public void appendChild(Node child) {
		children.add(child);
	}

	public void setLineno(int lineno) {
		this.lineno = lineno;
	}

	public void setLine(String line) {
		this.line = line;
	}

	private void toString(StringBuilder sb, int deep) {
		String prefix = "";
		for (int i = 0; i < deep; i++) {
			prefix += "  ";
		}
		Formatter formatter = new Formatter();
		sb.append(formatter.format("%04d " + prefix + "%s\n", lineno, line));
		formatter.close();
		//
		if (children != null)
			for (Node child : children) {
				child.toString(sb, deep + 1);
			}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		toString(sb, 0);
		return sb.toString();
	}
}
